package com.ruoyi.web.controller.device;

import java.util.List;

import com.ruoyi.common.core.domain.entity.DeviceConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IDeviceConfigService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备配置Controller
 * 
 * @author ruoyi
 * @date 2022-09-06
 */
@Controller
@RequestMapping("/device/config")
public class DeviceConfigController extends BaseController
{
    private String prefix = "device/config";

    @Autowired
    private IDeviceConfigService deviceConfigService;

    @RequiresPermissions("device:config:view")
    @GetMapping()
    public String config()
    {
        return prefix + "/config";
    }

    /**
     * 查询设备配置列表
     */
    @RequiresPermissions("device:config:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DeviceConfig deviceConfig)
    {
        startPage();
        List<DeviceConfig> list = deviceConfigService.selectDeviceConfigList(deviceConfig);
        return getDataTable(list);
    }

    /**
     * 导出设备配置列表
     */
    @RequiresPermissions("device:config:export")
    @Log(title = "设备配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DeviceConfig deviceConfig)
    {
        List<DeviceConfig> list = deviceConfigService.selectDeviceConfigList(deviceConfig);
        ExcelUtil<DeviceConfig> util = new ExcelUtil<DeviceConfig>(DeviceConfig.class);
        return util.exportExcel(list, "设备配置数据");
    }

    /**
     * 新增设备配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备配置
     */
    @RequiresPermissions("device:config:add")
    @Log(title = "设备配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DeviceConfig deviceConfig)
    {
        return toAjax(deviceConfigService.insertDeviceConfig(deviceConfig));
    }

    /**
     * 修改设备配置
     */
    @RequiresPermissions("device:config:edit")
    @GetMapping("/edit/{deviceId}")
    public String edit(@PathVariable("deviceId") Long deviceId, ModelMap mmap)
    {
        DeviceConfig deviceConfig = deviceConfigService.selectDeviceConfigBydeviceId(deviceId);
        if(deviceConfig==null){
            deviceConfig =new DeviceConfig();
            deviceConfig.setDeviceId(deviceId);
        }
        mmap.put("deviceConfig", deviceConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备配置
     */
    @RequiresPermissions("device:config:edit")
    @Log(title = "设备配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DeviceConfig deviceConfig)
    {
        return toAjax(deviceConfigService.updateDeviceConfig(deviceConfig));
    }

    /**
     * 删除设备配置
     */
    @RequiresPermissions("device:config:remove")
    @Log(title = "设备配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(deviceConfigService.deleteDeviceConfigByConfigIds(ids));
    }
}
